package com.example.zhou.fragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.example.zhou.adapter.Decoration.MyDecoration;
import com.example.zhou.adapter.MusicInfoAdapter;
import com.example.zhou.androidmusic.R;
import com.example.zhou.filehelper.AlbumUtil;
import com.example.zhou.filehelper.FileToJson;
import com.example.zhou.musicmodel.MusicBean;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * to handle interaction events.
 * Use the {@link MusicListFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MusicListFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    LinearLayoutManager layoutManager;
    List<MusicBean> list = new ArrayList<>();
    private MusicInfoAdapter musicInfoAdapter;

    public MusicListFragment() {
        // Required empty public constructor
    }

    private String musicInfo;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment MusicListFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MusicListFragment newInstance() {
        MusicListFragment fragment = new MusicListFragment();
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       /* if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }*/


    }


    /*获取音乐列表*/
    private List<MusicBean> getJsonArray() {
        musicInfo = FileToJson.read(getContext());
        if (TextUtils.isEmpty(musicInfo)) {
            FileToJson.writeToFile(getContext());
            musicInfo = FileToJson.read(getContext());
        }
        Log.e("music", musicInfo);
        return JSON.parseArray(musicInfo, MusicBean.class);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_music_list, container, false);
        ButterKnife.bind(this, view);
        layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);

        List<MusicBean> musicBeen = getJsonArray();
        Collections.sort(musicBeen);

        recyclerView.setLayoutManager(layoutManager);
        musicInfoAdapter = new MusicInfoAdapter(getContext(), musicBeen);
        recyclerView.setAdapter(musicInfoAdapter);
        MyDecoration myDecoration = new MyDecoration(getContext(), LinearLayoutManager.VERTICAL);
        recyclerView.addItemDecoration(myDecoration);
        layoutManager.setSmoothScrollbarEnabled(true);
        return view;
    }


    /*public List<Bitmap> getBitMaps(List<MusicBean> musicBeen) {
        List<Bitmap> bitmaps = new ArrayList<>();
        for (MusicBean musicBean : musicBeen) {
            bitmaps.add(AlbumUtil.getMusicAlbum(new File(musicBean.getFilepath())));
        }
        return null;
    }*/

    // TODO: Rename method, update argument and hook method into UI event

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        /*if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }*/
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

}
